package edu.iyte.ceng431.hw2.pos.model;

import edu.iyte.ceng431.hw2.pos.db.Insert;
import java.sql.SQLException;
import java.util.Date;
import java.util.Observable;

import edu.iyte.ceng431.hw2.pos.db.Search;

public class Register extends Observable implements IRegister {

    private int id;
    private double initialBalance = 0;
    private Date initialDate;
    private double balance;
    private Cashier cashier;
    private Sale currentsale;

    public Register(int id, double balance, Cashier cas) {
        super();
        this.id = id;
        this.cashier = cas;
        this.initialBalance = balance;
        this.balance = initialBalance;
        this.initialDate = new Date();
    }

    public int getCashierId() {
        return cashier.getId();
    }

    public Sale getCurrentsale() {
        return currentsale;
    }

    public void setCurrentsale(Sale currentsale) {
        this.currentsale = currentsale;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void addBalance(double add)
    {
    	this.balance += add;
    }
    
    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public void setCashier(Cashier cashier) {
        this.cashier = cashier;
    }

    public Cashier getCashier() {
        return cashier;
    }

    @Override
    public void makeSale() {
        if (currentsale != null) {
            Insert.insertSale(currentsale);
            this.balance += currentsale.calculateCashTotal();
        }
        currentsale = new Sale( this, this.cashier);
        setChanged();
        notifyObservers();

    }

    @Override
    public void notifyNewSaleLineItem(Product product, int quantity) {
        currentsale.addSaleLineItem(product, quantity);
    }

    @Override
    public boolean endSale() {
        if (currentsale.saleComplete()) {
            this.balance += currentsale.calculateTotal();
            return true;
        }
        return currentsale.saleComplete();
    }

    @Override
    public boolean balanceRegister() throws SQLException {
        double total = Search.getTotalCash(initialDate, new Date(), this);

        if (this.balance - total == this.initialBalance) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void receivePayment(double amount, int paymentType, String cardNumber)
            throws Exception {

        if (paymentType == 1) {
            currentsale.addPayment(new Cash(amount));
        } else if (paymentType == 2) {
            currentsale.addPayment(new CreditCard(amount, cardNumber));
        } else {
            throw new Exception("Unsopported Payment Type");
        }

    }

    /**
     * @return the initialBalance
     */
    public double getInitialBalance() {
        return initialBalance;
    }

    /**
     * @param initialBalance the initialBalance to set
     */
    public void setInitialBalance(double initialBalance) {
        this.initialBalance = initialBalance;
    }

    public double calculateTotalOfSales() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
